﻿using System.Windows;

namespace YourLastOptionsDialog.ViewModels.Options
{
    /// <summary>
    /// An option that represents a boolean value.
    /// </summary>
    public class BooleanOption : Option
    {
        private bool _userMustConfirmActivation;
        private bool _userMustConfirmDeactivation;
        private string _activationPrompt;
        private string _deactivationPrompt;

        /// <summary>
        /// Gets or sets a value indicating whether the underlying value of this option is true or false. Retrieved and written from and to the original source.
        /// Note: the setter shows a confirmation dialog, if this has been configured for the option.
        /// </summary>
        public bool Value
        {
            get
            {
                return (bool)GetAccessor();
            }

            set
            {
                if (Value && !value)
                {
                    // user is turning the option off
                    if (UserMustConfirmDeactivation)
                    {
                        if (MessageBox.Show(DeactivationPrompt, DisplayName, MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                        {
                            // we don't actually change the value, so raise the changed event to signal this
                            RaisePropertyChanged("Value");
                            return;
                        }
                    }
                }

                if (!Value && value)
                {
                    // user is turning the option on
                    if (UserMustConfirmActivation)
                    {
                        if (MessageBox.Show(ActivationPrompt, DisplayName, MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                        {
                            // we don't actually change the value, so raise the changed event to signal this
                            RaisePropertyChanged("Value");
                            return;
                        }
                    }
                }

                SetAccessor(value);
                RaisePropertyChanged("Value");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the user must confirm activation of this value (i.e. changing the value from <c>false</c> to <c>true</c>).
        /// </summary>
        /// <value>
        /// <c>true</c> if the user must confirm activation; otherwise, <c>false</c>.
        /// </value>
        public bool UserMustConfirmActivation
        {
            get
            {
                return _userMustConfirmActivation;
            }

            set
            {
                if (_userMustConfirmActivation != value)
                {
                    _userMustConfirmActivation = value;
                    RaisePropertyChanged("UserMustConfirmActivation");
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the user must confirm deactivation of this value (i.e. changing the value from <c>true</c> to <c>false</c>).
        /// </summary>
        /// <value>
        /// <c>true</c> if the user must confirm deactivation; otherwise, <c>false</c>.
        /// </value>
        public bool UserMustConfirmDeactivation
        {
            get
            {
                return _userMustConfirmDeactivation;
            }

            set
            {
                if (_userMustConfirmDeactivation != value)
                {
                    _userMustConfirmDeactivation = value;
                    RaisePropertyChanged("UserMustConfirmDeactivation");
                }
            }
        }

        /// <summary>
        /// Gets or sets the activation prompt. This is only used if <see cref="UserMustConfirmActivation"/> is set to true.
        /// </summary>
        public string ActivationPrompt
        {
            get
            {
                return _activationPrompt;
            }

            set
            {
                if (_activationPrompt != value)
                {
                    _activationPrompt = value;
                    RaisePropertyChanged("ActivationPrompt");
                }
            }
        }

        /// <summary>
        /// Gets or sets the deactivation prompt. This is only used if <see cref="UserMustConfirmDeactivation"/> is set to true.
        /// </summary>
        public string DeactivationPrompt
        {
            get
            {
                return _deactivationPrompt;
            }

            set
            {
                if (_deactivationPrompt != value)
                {
                    _deactivationPrompt = value;
                    RaisePropertyChanged("DeactivationPrompt");
                }
            }
        }
    }
}